package iet.jxufe.cn.android.productanti_counterfeitingtraceabilitysystem.activity.secondActivity;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import iet.jxufe.cn.android.productanti_counterfeitingtraceabilitysystem.R;
import iet.jxufe.cn.android.productanti_counterfeitingtraceabilitysystem.activity.ActivityLogin;
import iet.jxufe.cn.android.productanti_counterfeitingtraceabilitysystem.activity.ActivityProducer;
import iet.jxufe.cn.android.productanti_counterfeitingtraceabilitysystem.entity.User;
import iet.jxufe.cn.android.productanti_counterfeitingtraceabilitysystem.service.serviceConnection.LoginServiceConnection;
import iet.jxufe.cn.android.productanti_counterfeitingtraceabilitysystem.service.serviceImpl.LoginService;
import iet.jxufe.cn.android.productanti_counterfeitingtraceabilitysystem.utils.JSONObjectUtils;

public class ActivityModifyProfile extends AppCompatActivity {

    private EditText etUsername, etPassword, etEmail;
    private Button btnSubmit, btnBack;
    private User user;

    // 登录相关服务类
    private LoginServiceConnection loginConnection;
    // 处理后端传来的JSONObject
    private JSONObjectUtils JSONObjectUtils = new JSONObjectUtils();

    @Override
    protected void onStart() {
        super.onStart();
        loginConnection = new LoginServiceConnection(service -> {
            user = service.getUserInfo(user.getUserId());
            etUsername.setText(user.getUsername());
            etPassword.setText(user.getPassword());
            etEmail.setText(user.getEmail());
        });
        Intent intent = new Intent(this, LoginService.class);
        bindService(intent, loginConnection, BIND_AUTO_CREATE);
    }

    @Override
    protected void onStop() {
        super.onStop();
        unbindService(loginConnection);
    }


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_modify_profile);
        user = new User();
        init();


        // 初始化输入框和按钮
        etUsername = findViewById(R.id.et_username);
        etPassword = findViewById(R.id.et_password);
        etEmail = findViewById(R.id.et_email);
        btnSubmit = findViewById(R.id.btn_submit);
        btnBack = findViewById(R.id.btn_back);

        // 设置返回按钮的点击事件
        btnBack.setOnClickListener(v -> {
            finish();
        });

        // 设置提交按钮的点击事件
        btnSubmit.setOnClickListener(v -> {
            user.setUsername(etUsername.getText().toString());
            user.setPassword(etPassword.getText().toString());
            user.setEmail(etEmail.getText().toString());
            loginConnection.getService().updateUserInfo(user);
            Intent intent = new Intent(this, ActivityProducer.class);
            startActivity(intent);
            finish();
        });
    }

    private void init(){
        SharedPreferences sharedPreferences = getSharedPreferences("ACCOUNT_REMEMBER", MODE_PRIVATE);
        Log.d("userId",sharedPreferences.getInt("userId",-1) + " ");
        user.setUserId(sharedPreferences.getInt("userId",-1));
        if (user.getUserId() == -1){
            Toast.makeText(this, "非法登录！", Toast.LENGTH_SHORT).show();
            Intent intent = new Intent(this, ActivityLogin.class);
            startActivity(intent);
            return;
        }

    }
}
